package edu.tjufe.llp.controller;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.tjufe.llp.common.Result;
import edu.tjufe.llp.entity.ChargeArea;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import edu.tjufe.llp.entity.Car;
import edu.tjufe.llp.service.ICarService;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 电动车Controller
 * 
 * @author llp
 * @date 2022-12-25
 */
@RestController
@RequestMapping("/car")
public class CarController
{
    @Autowired
    private ICarService carService;

    /**
     * 查询电动车列表
     */
    @GetMapping("/list")
    public Result getCarList(@RequestParam Integer pageNum,
                              @RequestParam Integer pageSize,
                              @RequestParam(defaultValue = "") String carCode,
                             @RequestParam(defaultValue = "") String ownerName,
                             @RequestParam(defaultValue = "") String ownerTel){
        IPage<Car> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Car> queryWrapper = new QueryWrapper<>();
        if (!"".equals(carCode)) {
            queryWrapper.like("car_code",carCode);
        }
        if (!"".equals(ownerName)) {
            queryWrapper.like("owner_name",ownerName);
        }
        if (!"".equals(ownerTel)) {
            queryWrapper.like("owner_tel",ownerTel);
        }

        return Result.success(carService.page(page,queryWrapper));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String carCode,
                           @RequestParam(defaultValue = "") String ownerName,
                           @RequestParam(defaultValue = "") String ownerTel){
        return Result.success(carService.findPage(new Page<>(pageNum, pageSize), carCode,ownerName,ownerTel));
    }

    /**
     * 查询电动车总列表
     */
    @GetMapping("/carTotal")
    public Result findAll(Car car) {
        List<Car> list = carService.selectCarList(car);
        return Result.success(list);
    }
    /**
     * 导出电动车列表
     */
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Car car)
//    {
//        List<Car> list = carService.selectCarList(car);
//        ExcelUtil<Car> util = new ExcelUtil<Car>(Car.class);
//        util.exportExcel(response, list, "电动车数据");
//    }

    /**
     * 获取电动车详细信息
     */
    @GetMapping(value = "/{carId}")
    public Result getInfo(@PathVariable("carId") Integer carId)
    {
        return Result.success(carService.selectCarByCarId(carId));
    }

    /**
     * 新增电动车
     */
    @PostMapping
    public Result add(@RequestBody Car car)
    {
        return Result.toJudge(carService.insertCar(car));
    }

    /**
     * 修改电动车
     */
    @PutMapping
    public Result edit(@RequestBody Car car)
    {
        return Result.toJudge(carService.updateCar(car));
    }

    /**
     * 删除电动车
     */
	@DeleteMapping("/{carIds}")
    public Result remove(@PathVariable Integer[] carIds)
    {
        return Result.toJudge(carService.deleteCarByCarIds(carIds));
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response) throws Exception {
        // 从数据库查询出所有的数据
        List<Car> list = carService.list();
        // 通过工具类创建writer 写出到磁盘路径
//        ExcelWriter writer = ExcelUtil.getWriter(filesUploadPath + "/用户信息.xlsx");
        // 在内存操作，写出到浏览器
        ExcelWriter writer = ExcelUtil.getWriter(true);
        //自定义标题别名
        writer.addHeaderAlias("carId", "carId");
        writer.addHeaderAlias("carCode", "车牌号");
        writer.addHeaderAlias("ownerName", "车主姓名");
        writer.addHeaderAlias("ownerSex", "车主性别");
        writer.addHeaderAlias("ownerTel", "车主号码");
        writer.addHeaderAlias("carRemark", "备注");

        // 一次性写出list内的对象到excel，使用默认样式，强制输出标题
        writer.write(list, true);

        // 设置浏览器响应的格式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("充电区域信息", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");

        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        out.close();
        writer.close();

    }
}
